package com.hly.musicbackground.model.index.rest;

import com.hly.musicbackground.Final.RedisFinal;
import com.hly.musicbackground.filter.AuthRoot;
import com.hly.musicbackground.model.index.VO.HomeVO;
import com.hly.musicbackground.model.index.VO.LikeRecommendVO;
import com.hly.musicbackground.model.index.VO.RecommendVO;
import com.hly.musicbackground.model.index.params.EditHomeParams;
import com.hly.musicbackground.model.index.params.OtherParams;
import com.hly.musicbackground.model.index.service.HomeService;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页
 * */
@RestController
@RequestMapping("/api/home")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "首页")
public class HomeController {

    private final HomeService homeService;
    private final StringRedisTemplate redisTemplate;

    /**
     * 获取推荐数据【即将淘汰】
     * */
    @GetMapping("/recommend")
    @ApiOperation(value = "获取推荐数据【即将淘汰】")
    public Result<List<RecommendVO>> getRecommend(){
        return Result.ok(homeService.getRecommend());
    }

    /**
     * 获取个性化推荐
     * */
    @GetMapping("/like")
    @ApiOperation(value = "获取个性化推荐")
    public Result<LikeRecommendVO> getLike(OtherParams otherParams){
        return Result.ok(homeService.getLike(otherParams));
    }

    /**
     * 获取推荐数据目录
     * */
    @GetMapping("/directory")
    @ApiOperation(value = "获取推荐数据目录")
    public Result<List<HomeVO>> getDirectory(){
        return Result.ok(homeService.getDirectory());
    }

    /**
     * 获取推荐数据详情
     * */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获取推荐数据详情")
    public Result<RecommendVO> getDetail(@PathVariable int id){
        return Result.ok(homeService.getDetail(id));
    }

    @PostMapping("/edit")
    @ApiOperation(value = "新增/修改Home[管理端]")
    @AuthRoot
    public Result add(@RequestBody EditHomeParams params){
        homeService.editHome(params);
        return Result.ok();
    }

    /**
     * 删除Home
     * */
    @PostMapping("/del")
    @ApiOperation(value = "删除Home[管理端]")
    @AuthRoot
    public Result<RecommendVO> del(int id){
        homeService.delHomeByIdAll(id);
        return Result.ok();
    }

    /**
     * 刷新缓存
     * */
    @GetMapping("/refresh")
    @ApiOperation(value = "刷新缓存[管理端]")
    @AuthRoot
    public Result refresh(@RequestParam int id){
        redisTemplate.delete(RedisFinal.HOME_GET_DETAIL_ID + id);
        return Result.ok();
    }
}
